package com.videoaggre.zcl.helper

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.google.android.material.tabs.TabLayout
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.videoaggre.zcl.utils.Logger
import com.videoaggre.zcl.utils.Utils
import java.lang.ref.WeakReference
import java.util.*

interface OnValidationResultListener {
    fun enable(enable: Boolean)
}

interface OnValidationInterceptListener {
    fun validator(view: View): Boolean? // 返回结果就有自己校验，返回空交由工具类判断
}

class TextWatcher(
    private val onTextChanged: ((CharSequence, Int, Int) -> Unit)? = null,
    private val beforeTextChanged: ((CharSequence, Int, Int) -> Unit)? = null,
    private val afterTextChanged: ((Editable) -> Unit)? = null
) : TextWatcher {

    override fun afterTextChanged(s: Editable) {
        afterTextChanged?.invoke(s)
    }

    override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
        beforeTextChanged?.invoke(s, start, count)
    }

    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
        onTextChanged?.invoke(s, start, count)
    }
}

interface RefreshListener {
    fun stopRefresh()
}

class SmartRefreshListener(refreshLayout: RefreshLayout) : RefreshListener {
    private var wfRefreshLayout: WeakReference<RefreshLayout> = WeakReference(refreshLayout)
    override fun stopRefresh() {
        try {
            wfRefreshLayout.get()?.finishRefresh(0)
            wfRefreshLayout.get()?.finishLoadMore(0)
        } catch (e: Exception) {
            Logger.e(e)
        }
    }
}

interface OnStartActivityListener {
    fun goActicity(clazz: Class<*>, bundle: Bundle? = null, requestCode: Int? = null)
    fun goActicityFinish(clazz: Class<*>, bundle: Bundle? = null, requestCode: Int? = null)
}

/*class OnTabSelectListener(
    var onTabReselect: ((position: Int) -> Unit)? = null,
    var onTabSelect: ((position: Int) -> Unit)? = null
) : OnTabSelectListener {
    override fun onTabSelect(position: Int) {
        onTabSelect?.invoke(position)
    }

    override fun onTabReselect(position: Int) {
        onTabReselect?.invoke(position)
    }

}*/

/*
interface OnFileUploadListener {
    fun onStart()
    fun onSuccess(cosPath: MutableList<ImageUploadBean>)
    fun onFail(e: Throwable?)
    fun onPublishProgress(progress: Int)
}
*/

class OnTabSelectedListener(
    var onTabReselected: ((tab: TabLayout.Tab) -> Unit)? = null,
    var onTabUnselected: ((tab: TabLayout.Tab) -> Unit)? = null,
    var onTabSelected: ((tab: TabLayout.Tab) -> Unit)? = null
) : TabLayout.OnTabSelectedListener {
    override fun onTabReselected(tab: TabLayout.Tab) {
        onTabReselected?.invoke(tab)
    }

    override fun onTabUnselected(tab: TabLayout.Tab) {
        onTabUnselected?.invoke(tab)
    }

    override fun onTabSelected(tab: TabLayout.Tab) {
        onTabSelected?.invoke(tab)
    }

}

interface OnCheckBean {
    fun showName(): CharSequence
    fun check(): Boolean
    fun changeCheck(check: Boolean)
}
class OnSearchEditorActionListener(private val onSuccess: ((String) -> Unit)? = null) : TextView.OnEditorActionListener {
    override fun onEditorAction(textView: TextView, actionId: Int, event: KeyEvent?): Boolean {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//            if (TextUtils.isEmpty(textView.text)) {
//                toastMessageLong("请输入搜索关键字")
//                return true
//            }
            Utils.hideSoftInput(textView)
            onSuccess?.invoke(textView.text.toString())
            return true
        }
        return false
    }

}
